package com.zhouxiaoge.jenkins.web;

import com.offbytwo.jenkins.model.Computer;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.zhouxiaoge.jenkins.core.service.JenkinsManageJobService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 周明阳
 * @date 2019/7/18 09:41
 * Jenkins的Job操作Controller
 */

@Api(tags = "jenkins操作Job接口对接")
@RestController
@RequestMapping(value = "/jenkins/job")
@Slf4j
public class JenkinsJobController {

    private final JenkinsManageJobService jenkinsManageJobService;

    public JenkinsJobController(JenkinsManageJobService jenkinsManageJobService) {
        this.jenkinsManageJobService = jenkinsManageJobService;
    }

    //获取所有Jobs信息
    @GetMapping("/acquireAllJobs")
    public Map<String, Computer> acquireAllJobs() {
        return jenkinsManageJobService.acquireJobs();
    }

    //创建Job
    @PostMapping("/createBuildJob")
    public String createBuildJob(String jobName, String jobDescribe) {
        // 验证jobName是否已存在待完善
        jenkinsManageJobService.createBuild(jobName, jobDescribe);
        return "SUCCESS";
    }

    //获取备注
    @GetMapping("/acquireRemarks")
    @ResponseBody
    public String acquireRemarks(String jobName) {
        JobWithDetails jobWithDetails = jenkinsManageJobService.acquireRemarks(jobName);
        return jobWithDetails.getDescription();
    }

    //修改Job
    @PostMapping("/updateBuildJob")
    public String updateBuildJob(String jobName, String jobDescribe) {
        //General
        //源码管理
        //构建触发器
        //构建环境
        //构建
        //构建后操作
        jenkinsManageJobService.updateBuildJob(jobName, jobDescribe);
        return "SUCCESS";
    }

    //执行构建Job
    @PostMapping("/executeBuildJob")
    public String executeBuildJob(String jobName) {
        jenkinsManageJobService.executeBuildJob(jobName);
        return "SUCCESS";
    }

    //获取控制台输出
    @GetMapping("/acquireConsoleOutput")
    public String acquireConsoleOutput(String jobName) {
        return jenkinsManageJobService.acquireConsoleOutput(jobName);
    }
}
